
//#include "head.h"
//#include <stdio.h>
#include <signal.h>

#include <iostream>

#include <workflow/WFFacilities.h>
#include <workflow/WFTaskFactory.h>
#include <workflow/HttpMessage.h>
#include <workflow/HttpUtil.h>
using std::cout;
using std::endl;
using std::vector;
using std::string;

//
//初始化计数器的值为1
static WFFacilities::WaitGroup waitGroup(1);

void sighandler(int num)
{
    printf(">> sig num %d is comming\n", num);
    //调用一次done方法，计数器的值减1
    waitGroup.done();
}

void parallelCallback(const ParallelWork * )
{   printf("parallelCallback is running.\n");   }

void parallelSeriesCallback(const SeriesWork *)
{   printf("parallelSeriesCallback is running.\n"); }

void outsideSeriesCallback(const SeriesWork *) 
{   printf("outsideSeriesCallback is running.\n");  }

void httpCallback(WFHttpTask * )
{   printf("httpCallback is running.\n");   

}

void test0()
{
    signal(SIGINT, sighandler);

    vector<string> webs{
        "http://www.baidu.com",
        "http://www.zhihu.com",
        "http://www.cskaoyan.com"
    };

    auto parallelWork = Workflow::create_parallel_work(parallelCallback);
    for(size_t i = 0; i < webs.size(); ++i) {
        auto httpTask = WFTaskFactory::create_http_task(webs[i], 1, 1, httpCallback);
        //设置任务的属性
        httpTask->get_req()->set_header_pair("User-Agent", "Workflow client");

        auto series = Workflow::create_series_work(httpTask, parallelSeriesCallback);
        parallelWork->add_series(series);
    }

    //方案一：启动并行任务
    //parallelWork->start();//
    
    //方案二: 创建序列，将parallelWork放入序列中，同时注册序列的回调函数
    auto seriesOfParallelWork = Workflow::create_series_work(parallelWork, outsideSeriesCallback);
    seriesOfParallelWork->start();


    //当调用wait方法会阻塞当前线程
    waitGroup.wait();
    cout << "test0 exit" << endl;
}


int main()
{
    test0();
    return 0;
}

